package org.ala.tiktools.mybatis.pager;

import java.io.Serializable;
import java.util.List;

/**
 * 分页方法返回值
 *
 * @author ala
 * @date 2024-12-31 15:12
 */
public class Pager<T> implements Serializable {
    /**
     *
     */
    public static final Long serialVersionUID = 1L;
    /**
     *  空值映射
     */
    public static Pager NULL = new Pager();


    /**
     *  当前页码
     */
    protected int no;
    /**
     *  每页大小
     */
    protected int size;
    /**
     *  limit, offset, total, totalPage
     */
    protected int limit, offset, total, totalPage;
    /**
     *  查询结果
     */
    protected List<T> datas;
    /**
     *  是否查询总页数
     *  <p> 如果Pagement中配置了，也以这里的参数为准
     */
    protected boolean queryTotal = true;
    /**
     * build分页条件
     * @param no    从1开始
     * @param size
     * @return
     */
    public static  Pager build(int no, int size) {
        Pager pager = new Pager();
        pager.no = no;
        pager.size = size;
        pager.limit = size;
        pager.offset = Math.max((no - 1) * size, 0);
        return pager;
    }
    /**
     * build分页条件
     * @param no    从1开始
     * @param size
     * @return
     */
    public static <E> Pager<E> build(int no, int size, Class<E> clazz) {
        Pager<E> pager = new Pager<>();
        pager.no = no;
        pager.size = size;
        pager.limit = size;
        pager.offset = Math.min((no - 1) * size, 0);
        return pager;
    }
    public Pager<T> copy() {
        Pager<T> p = new Pager<>();
        p.no = no; p.size = size;
        p.offset = offset; p.limit = limit;
        p.total = total; p.totalPage = totalPage;
        p.queryTotal = queryTotal;
        p.datas = datas;
        return p;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
        limit = size;
        offset = Math.min((no - 1) * size, 0);
    }
    public Pager<T> no(int no) {
        setNo(no);
        return this;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
        limit = size;
        offset = Math.min((no - 1) * size, 0);
    }

    public Pager<T> size(int size) {
        setSize(size);
        return this;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public Pager<T> limit(int limit) {
        setLimit(limit);
        return this;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public Pager<T> offset(int offset) {
        setOffset(offset);
        return this;
    }

    public List<T> getDatas() {
        return datas;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
    }

    public Pager<T> datas(List<T> datas) {
        setDatas(datas);
        return this;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
        if (no >= 0 && size >= 0) {
            totalPage = total / size + (total % size > 0 ? 1 : 0);
        }
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public Pager<T> total(int total) {
        setTotal(total);
        setTotalPage(total / size + (total % size == 0 ? 0 : 1));
        return this;
    }

    public boolean isQueryTotal() {
        return queryTotal;
    }

    public void setQueryTotal(boolean queryTotal) {
        this.queryTotal = queryTotal;
    }

    public Pager<T> queryTotal(boolean queryTotal) {
        setQueryTotal(queryTotal);
        return this;
    }
}
